package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.ProfessorDAO;
import br.urca.model.Professor;

@Transactional
public class JPAProfessorDAO implements ProfessorDAO {
	
	private EntityManager manager;
	
	public JPAProfessorDAO() {
		this(null);
	}
	
	@Inject
	public JPAProfessorDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Professor> todos() {
		return this.manager.createQuery("SELECT p FROM Professor p", Professor.class)
				.getResultList();
	}

	@Override
	public Professor buscarPorId(String codigo) {
		return this.manager.createQuery("SELECT p FROM Professor p "
				+ "WHERE p.id = :codigo", Professor.class)
				.setParameter("codigo", codigo)
				.getSingleResult();
	}

	@Override
	public Professor buscarPorMatricula(String matricula) {
		return this.manager.createQuery("SELECT p FROM Professor p "
				+ "WHERE p.matricula = :matricula", Professor.class)
				.setParameter("matricula", matricula)
				.getSingleResult();
	}

	@Override
	public Professor buscarPorMatriculaSenha(String matricula, String senha) {
		return this.manager.createQuery("SELECT p FROM Professor p "
				+ "WHERE p.matricula = :matricula "
				+ "AND p.senha = :senha ", Professor.class)
				.setParameter("matricula", matricula)
				.setParameter("senha", senha)
				.getSingleResult();
	}

}
